package com.sy.controller;

import com.github.pagehelper.Page;
import com.sy.dto.*;
import com.sy.entity.ClassMsg;
import com.sy.entity.Student;
import com.sy.entity.StudentDownloadCourseware;
import com.sy.service.impl.BaseServiceImpl;
import com.sy.vo.ChangePwdVo;
import com.sy.vo.ScoreVo;
import com.sy.vo.SearchStuByKeywordsVo;
import com.sy.vo.TeaInfoVo;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
public class TeacherController extends BaseController {
    //获得老师的信息
    @RequestMapping(value = "teacher",method = RequestMethod.GET)
    public TeaInfo getTeaInfo(){
        Integer sessionUserId = getSessionUserId();
        TeaInfo teaInfo = teacherService.getTeaInfo(String.valueOf(sessionUserId));
        teaInfo.setId(sessionUserId);
        return teaInfo;
    }
    //更新老师的信息
    @RequestMapping(value = "teacher",method = RequestMethod.PUT,params = {"type=updateTeaInfo"})
    public boolean updateTeaInfo(@RequestBody TeaInfoVo teaInfoVo){
        return teacherService.updateTeaInfo(teaInfoVo);
    }
    //更改老师的密码
    @RequestMapping(value="teacher",method = RequestMethod.PUT,params = {"type=changeTeaPwd"})
    public boolean changeTeaPwd(@RequestBody ChangePwdVo changePwdVo){
        UserSession sessionUser = getSessionUser();
        changePwdVo.setId(String.valueOf(sessionUser.getId()));
        changePwdVo.setIdentity(sessionUser.getIdentity());
        return loginService.changePwd(changePwdVo);

    }
    //根据老师id获取学生列表
    @RequestMapping(value = "students",method = RequestMethod.GET,params = {"type=myStudents","page","limit"})
    public Table getStudentsTableByTeacherId(Integer page,Integer limit){
        //获取当前老师的id
        Integer teacherId = getSessionUserId();
        Page<StudentMagTable> studentList = teacherService.getStudentListByTeacherIdForPage(teacherId,page,limit);
        return getTable(studentList);
    }

    //根据关键词搜索学生列表
    @RequestMapping(value = "students",method = RequestMethod.GET,params = {"type=myStudents","page","limit","studyNo","name"})
    public  Table getStudentTableByKeywords(Integer page,Integer limit,String studyNo,String name) throws UnsupportedEncodingException {

        //请求url中的中文解析
        String StudentNo = URLDecoder.decode(name, "UTF-8");
        SearchStuByKeywordsVo vo = new SearchStuByKeywordsVo();
        //获取当前老师的id
        Integer teacherId = getSessionUserId();
        vo.setTeacherId(teacherId);
        vo.setPage(page);
        vo.setLimit(limit);
        vo.setStudyNo(studyNo);
        vo.setName(StudentNo);
        Page<StudentMagTable> studentList = teacherService.getStudentListByKeyWords(vo);
        return getTable(studentList);
    }
    //获得分数表用于批改
    @RequestMapping(value = "scores",method = RequestMethod.GET,params = {"page","limit"})
    public Table getCheckTable(Integer page,Integer limit){
        //获取当前老师的id
        Integer teacherId = getSessionUserId();
        Page<HomeworkCheckTab> checkHomeworkTable = teacherService.getCheckHomeworkTable(teacherId, page, limit);
        return getTable(checkHomeworkTable);
    }
    //批改作业详情
    @RequestMapping(value = "score/{id}",method = RequestMethod.GET,params = {"homeworkId","studentId"})
    public CheckHomeworkDetailTab getCheckHomeworkDetail(@PathVariable("id")Integer scoreId, Integer homeworkId, Integer studentId){
        return teacherService.getCheckHomeworkDetail(scoreId,homeworkId,studentId);
    }

    //批改简答题给出分数
    @RequestMapping(value = "score/{id}",method = RequestMethod.PUT)
    public void putScore(@PathVariable("id")Integer scoreId, @RequestBody ScoreVo scoreVo){
        //获取当前老师的id
        Integer teacherId = getSessionUserId();
        teacherService.inputScore(teacherId,scoreId,scoreVo);
    }

    //学生根据自己所属老师去查询老师上传的文件,并开启分页
    @RequestMapping(value = "teacher",method = RequestMethod.GET,params = {"type=getStudyFile","page", "limit"})
    public  Table getTeacherUploadFile(Integer page,Integer limit){
//       先得到老师的id
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id = session.getId();
//       再根据老师的id去查询到老师上传的文件
        Page<StudentDownloadCourseware> teacherUploadFile = studentService.getTeacherUploadFile(id, "", page, limit);
        return getTable(teacherUploadFile);

    }


    //根据关键词搜索查询
    @RequestMapping(value = "teacher",method = RequestMethod.GET,params = {"type=getStudyFile","page", "limit","title"})
    public  Table getTeacherUploadFileByKeywords(Integer page,Integer limit,String title) throws UnsupportedEncodingException{
        String decode = URLDecoder.decode(title, "UTF-8");
//        先得到老师的id
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id = session.getId();
//        再根据老师的id去查询到老师上传的文件
        Page<StudentDownloadCourseware> teacherUploadFile = studentService.getTeacherUploadFile(id, decode, page, limit);
        return getTable(teacherUploadFile);
    }
}
